﻿using SuporeSliplampApp.Base;
using SuporeSliplampApp.Models;
using SuporeSliplampApp.Util;
using SuporeSliplampApp.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows.Media.Media3D;
using System.Collections.ObjectModel;
using static SuporeSliplampApp.Models.MainModel;
using System.Windows.Forms;
using HandyControl.Controls;
using Supore.APP.UI.Controls.Notify;
using SuporeSliplampApp.Service;
using System.Diagnostics.Eventing.Reader;

namespace SuporeSliplampApp.ViewModels
{
    public class MainViewModel : NotifyBase
    {
        public static event EventHandler<int> mainDelegate;

        static public MainViewModel mainViewModel = new MainViewModel();
        public static MainViewModel GetInstance()
        {
            return mainViewModel;
        }
        public MainModel mainModel { get; set; } = new MainModel();
        //public CameraModel camModel { get; set; } = new CameraModel();

        public PatientModel patientModel { get; set; } = new PatientModel();

        //public PatientModel PatientModel
        //{
        //    get { return patientModel; }
        //    set { patientModel = value; this.NotifyChanged(); }
        //} 

        public CameraModel _cameraModel { get; set; } = new CameraModel();



        private CommandBase _closeCommand;

        /// <summary>
        /// 关闭命令
        /// </summary>
        public CommandBase CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new CommandBase();
                    _closeCommand.DoExecute = new Action<object>(obj =>
                    {
                        if (System.Windows.MessageBox.Show("请确定退出?", "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                        {
                            (obj as System.Windows.Window).DialogResult = false;
                        }

                    });
                }
                return _closeCommand;
            }

        }
        private CommandBase _minCommand;

        /// <summary>
        /// 关闭命令
        /// </summary>
        public CommandBase MinCommand
        {
            get
            {
                if (_minCommand == null)
                {
                    _minCommand = new CommandBase();
                    _minCommand.DoExecute = new Action<object>(obj =>
                    {
                        (obj as System.Windows.Window).WindowState = WindowState.Minimized;

                    });
                }
                return _minCommand;
            }
        }

        private CommandBase _editCommand;

        public CommandBase EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new CommandBase();
                    _editCommand.DoExecute = new Action<object>(obj =>
                    {
                        string tem = obj as string;
                        if (string.IsNullOrEmpty(tem))
                            return;
                        else
                        {
                            mainModel.EditEnabled = true;
                            mainModel.ButtonWidth = 70;
                            mainDelegate?.Invoke(this, 1);
                        }
                    });
                }
                return _editCommand;
            }
        }

        private CommandBase _saveCommand;

        public CommandBase SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new CommandBase();
                    _saveCommand.DoExecute = new Action<object>(obj =>
                    {

                    });
                }
                return _saveCommand;
            }
        }



        private CommandBase _delCommand;

        public CommandBase DelCommand
        {
            get
            {
                if (_delCommand == null)
                {
                    _delCommand = new CommandBase();
                    _delCommand.DoExecute = new Action<object>(obj =>
                    {
                    });
                }
                return _delCommand;
            }
        }

        private Visibility patientVisibility = Visibility.Visible;

        public Visibility PatientVisibility
        {
            get { return patientVisibility; }
            set { patientVisibility = value; this.NotifyChanged(); }
        }

        private Visibility buttonVisibility = Visibility.Collapsed;

        public Visibility ButtonVisibility
        {
            get { return buttonVisibility; }
            set { buttonVisibility = value; this.NotifyChanged(); }
        }

        private bool isEditEnabled = false;

        public bool IsEditEnabled
        {
            get { return isEditEnabled; }
            set { isEditEnabled = value; this.NotifyChanged(); }
        }

        private Cursor myCursor = Cursors.No;

        public Cursor MyCursor
        {
            get
            {
                if (!IsEditEnabled)
                    myCursor = Cursors.No;
                else
                    myCursor = Cursors.Hand;
                return myCursor;
            }
            set { myCursor = value; this.NotifyChanged(); }
        }



        /// <summary>
        /// 快捷菜单上方按钮文字描述
        /// </summary>
        private string _topSubInfo;

        public string TopSubInfo
        {
            get { return _topSubInfo; }
            set { _topInfo = UIInfo.FAST_UP_NAME; }
        }

        /// <summary>
        /// 快捷菜单上方按钮文字描述
        /// </summary>
        private string _topInfo;

        public string TopInfo
        {
            get { return _topInfo; }
            set { _topInfo = UIInfo.FAST_UP_SUB_NAME; }
        }

        /// <summary>
        /// 快捷菜单下方按钮文字描述
        /// </summary>
        private string _belowInfo;

        public string BelowInfo
        {
            get { return _belowInfo; }
            set { _belowInfo = UIInfo.FAST_BELOW_SUB_NAME; }
        }

        /// <summary>
        /// 快捷菜单下方按钮文字描述
        /// </summary>
        private string _belowSubInfo;

        public string BelowSubInfo
        {
            get { return _belowSubInfo; }
            set { _belowSubInfo = UIInfo.FAST_BELOW_SUB_NAME; }
        }



        public MainViewModel()
        {
            this.NavPage("SuporeSliplampApp.Views.MainContent");
            //VM_Model = new ObservableCollection<Model>();
            //VM_Model.Add(new Model() {Path = @"C:\Users\14068\Desktop\test2.jpg" });
            mainModel.VM_Model = new ObservableCollection<Model>();
            for (int i = 0; i < 20; ++i)
            {
                mainModel.VM_Model.Add(new Model() { Path = @"C:\Users\14068\Desktop\test2.jpg" });
            }

        }

        private CommandBase _menuItemCommand;
        public CommandBase MenuItemCommand
        {
            get
            {
                if (_menuItemCommand == null)
                {
                    _menuItemCommand = new CommandBase();
                    _menuItemCommand.DoExecute = new Action<object>(obj =>
                    {
                        NavPage(obj.ToString());
                        if (obj.ToString() == Common.ViewClass.MAIN)
                        {
                            PatientVisibility = System.Windows.Visibility.Visible;
                            ButtonVisibility = System.Windows.Visibility.Collapsed;
                        }
                        else if (obj.ToString() == Common.ViewClass.CAMERA)
                        {
                            PatientVisibility = System.Windows.Visibility.Collapsed;
                            ButtonVisibility = System.Windows.Visibility.Visible;
                        }

                    });
                }
                return _menuItemCommand;
            }

        }

        private CommandBase _maxCommand;
        public CommandBase MaxCommand
        {
            get
            {
                if (_maxCommand == null)
                {
                    _maxCommand = new CommandBase();
                    _maxCommand.DoExecute = new Action<object>(obj =>
                    {
                        test(obj);
                    });
                }
                return _menuItemCommand;
            }

        }

        private CommandBase _addPatientCommand;
        public CommandBase AddPatientCommand
        {
            get
            {
                if (_addPatientCommand == null)
                {
                    _addPatientCommand = new CommandBase();
                    _addPatientCommand.DoExecute = new Action<object>(obj =>
                    {
                        addPatient();
                    });
                }
                return _addPatientCommand;
            }

        }

        private CommandBase _openPatientCommand;
        public CommandBase OpenPatientCommand
        {
            get
            {
                if (_openPatientCommand == null)
                {
                    _openPatientCommand = new CommandBase();
                    _openPatientCommand.DoExecute = new Action<object>(obj =>
                    {
                        OpenPatient();
                    });
                }
                return _openPatientCommand;
            }

        }

        private CommandBase _addUserCommand;
        public CommandBase AddUserCommand
        {
            get
            {
                if (_addUserCommand == null)
                {
                    _addUserCommand = new CommandBase();
                    _addUserCommand.DoExecute = new Action<object>(obj =>
                    {
                        AddUser();
                    });
                }
                return _addUserCommand;
            }

        }

        private void AddUser()
        {
            NewUserWindow newUserWindow = new NewUserWindow();
            newUserWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            newUserWindow.ShowDialog();
        }

        private CommandBase contextMenuCmd;

        public CommandBase ContextMenuCmd
        {
            get
            {
                if (contextMenuCmd == null)
                {
                    contextMenuCmd = new CommandBase();
                    contextMenuCmd.DoExecute = new Action<object>(obj =>
                    {
                        System.Windows.Controls.Button button = obj as System.Windows.Controls.Button;
                        System.Windows.Controls.ContextMenu contextMenu = button.ContextMenu;
                        contextMenu.PlacementTarget = button;
                        contextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
                        contextMenu.IsOpen = true;

                    });
                }
                return contextMenuCmd;
            }

        }

        private bool isContextMenuOpen = false;

        public bool IsContextMenuOpen
        {
            get { return isContextMenuOpen; }
            set { isContextMenuOpen = value; this.NotifyChanged(); }
        }



        private void addPatient()
        {
            NewPatientWindow newPatientWindow = new NewPatientWindow();
            newPatientWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            newPatientWindow.ShowDialog();
        }

        private void OpenPatient()
        {
            OpenPatients openPatient = new OpenPatients();
            openPatient.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            openPatient.ShowDialog();
        }


        public void test(object str)
        {
            var t = str as ContentControl;
            t.Width = 1920;
            t.Height = 1080;
        }

        private CommandBase _homeCommand;

        public CommandBase HomeCommand
        {
            get
            {
                _homeCommand = new CommandBase();
                _homeCommand.DoExecute = new Action<object>(obj => { this.NavPage("SuporeSliplampApp.Views.MainContent"); });

                return this._homeCommand;
            }

        }

        private CommandBase _checkCmd;
        public CommandBase CheckCmd
        {
            get
            {
                if (_checkCmd == null)
                {
                    _checkCmd = new CommandBase();
                    _checkCmd.DoExecute = new Action<object>(obj =>
                    {
                        if ((bool)obj)
                        {
                            System.Windows.MessageBox.Show("test");
                        }
                    });
                }
                return _checkCmd;
            }
        }

        private CommandBase _openCameraCommand;

        public CommandBase OpenCameraCommand
        {
            get
            {
                if (_openCameraCommand == null)
                {
                    _openCameraCommand = new CommandBase();
                    _openCameraCommand.DoExecute = new Action<object>(obj =>
                    {
                        Task.Run(() =>
                        {
                            this.mainModel.LableMsg = "正在打开相机.....";
                        });
                    });
                }
                return _openCameraCommand;
            }
        }

        private void test2(string name)
        {
            Type type = Type.GetType(name);

        }

        public void NavPage(string name)
        {
            Type type = Type.GetType(name);
            this.mainModel.MainContent = (System.Windows.UIElement)Activator.CreateInstance(type);
            this.mainModel.MainContent.Visibility = Visibility.Visible;

        }

        public void showWindow(string name)
        {
            Type type = Type.GetType(name);
            System.Windows.Window window = (System.Windows.Window)Activator.CreateInstance(type);
            window.Show();
        }
    }
}
